//package com.lzx.applib.utils;
//
//import java.util.List;
//
//import android.content.Context;
//import android.net.wifi.ScanResult;
//import android.net.wifi.WifiConfiguration;
//import android.net.wifi.WifiConfiguration.AuthAlgorithm;
//import android.net.wifi.WifiConfiguration.KeyMgmt;
//import android.net.wifi.WifiManager;
//import android.net.wifi.WifiManager.ActionListener;
//import android.text.TextUtils;
//
//public class WifiUtil {
//
//	private static final String TAG = "WifiUtil";
//
//	public static final int SECURITY_NONE = 0;
//	public static final int SECURITY_WEP = 1;
//	public static final int SECURITY_PSK = 2;
//	public static final int SECURITY_EAP = 3;
//
//	public static int getSecurity(ScanResult result) {
//		if (result.capabilities.contains("WEP")) {
//			return SECURITY_WEP;
//		} else if (result.capabilities.contains("PSK")) {
//			return SECURITY_PSK;
//		} else if (result.capabilities.contains("EAP")) {
//			return SECURITY_EAP;
//		}
//
//		return SECURITY_NONE;
//	}
//
//	public static String convertToQuotedString(String string) {
//		return "\"" + string + "\"";
//	}
//
//	public static WifiConfiguration getConfiguration(String passwd, ScanResult mScanResult) {
//		if (mScanResult == null) {
//			return null;
//		}
//
//		WifiConfiguration config = new WifiConfiguration();
//		config.SSID = convertToQuotedString(mScanResult.SSID);
//
//		int mSecureType = getSecurity(mScanResult);
//		switch (mSecureType) {
//		case SECURITY_NONE:
//			config.allowedKeyManagement.set(KeyMgmt.NONE);
//			break;
//		case SECURITY_WEP:
//			config.allowedKeyManagement.set(KeyMgmt.NONE);
//			config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
//			config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
//
//			config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
//			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
//			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
//			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
//			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
//			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//
//			if (passwd.length() != 0) {
//				int length = passwd.length();
//				// WEP-40, WEP-104, and 256-bit WEP (WEP-232?)
//				if ((length == 10 || length == 26 || length == 58)
//						&& passwd.matches("[0-9A-Fa-f]*")) {
//					config.wepKeys[0] = passwd;
//				} else {
//					config.wepKeys[0] = convertToQuotedString(passwd);
//				}
//			}
//			break;
//		case SECURITY_PSK:
//			config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
//
//			config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
//			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
//			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
//			config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
//			config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
//			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
//			config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
//			config.status = WifiConfiguration.Status.ENABLED;
//
//			if (passwd.length() != 0) {
//				if (passwd.matches("[0-9A-Fa-f]{64}")) {
//					config.preSharedKey = passwd;
//				} else {
//					config.preSharedKey = '"' + passwd + '"';
//				}
//			}
//			break;
//		default:
//			config = null;
//		}
//
//		return config;
//	}
//
//	public static void connect(Context context, int networkID, final ActionListener listener) {
//		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
//		wifiManager.connect(networkID, new ActionListener() {
//
//			@Override
//			public void onSuccess() {
//				LogUtil.i(TAG + "::onSuccess()");
//				if (listener != null) {
//					listener.onSuccess();
//				}
//			}
//
//			@Override
//			public void onFailure(int reason) {
//				LogUtil.i(TAG + "：：onFailure() [" + reason + "]");
//				if (listener != null) {
//					listener.onFailure(reason);
//				}
//			}
//		});
//		ToastUtil.getInstance().toast("连接中...");
//	}
//
//	public static void connect(Context context, String bssid, final ActionListener listener) {
//		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
//		List<WifiConfiguration> networks = wifiManager.getConfiguredNetworks();
//		for (WifiConfiguration config : networks) {
//			if (TextUtils.equals(config.BSSID, bssid)) {
//				WifiUtil.connect(context, config.networkId, listener);
//				return;
//			}
//		}
//	}
//
//	public static void connect(Context context, WifiConfiguration configuration, final ActionListener listener) {
//		WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
//		wifiManager.connect(configuration, new ActionListener() {
//
//			@Override
//			public void onSuccess() {
//				LogUtil.i(TAG + "::onSuccess()");
//				if (listener != null) {
//					listener.onSuccess();
//				}
//			}
//
//			@Override
//			public void onFailure(int reason) {
//				LogUtil.i(TAG + "：：onFailure() [" + reason + "]");
//				if (listener != null) {
//					listener.onFailure(reason);
//				}
//			}
//		});
//		ToastUtil.getInstance().toast("连接中...");
//	}
//
//	//OK
//	static WifiConfiguration createWifiInfo(ScanResult result, String Password, int type) {
//		String SSID = result.SSID;
//		type = getSecurity(result);
//		WifiConfiguration config = new WifiConfiguration();
//		config.allowedAuthAlgorithms.clear();
//		config.allowedGroupCiphers.clear();
//		config.allowedKeyManagement.clear();
//		config.allowedPairwiseCiphers.clear();
//		config.allowedProtocols.clear();
//		config.SSID = "\"" + SSID + "\"";
//
//		if (type == SECURITY_NONE) { // WIFICIPHER_NOPASS
//			config.wepKeys[0] = "";
//			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//			config.wepTxKeyIndex = 0;
//		} else if (type == SECURITY_WEP) { // WIFICIPHER_WEP
//			config.hiddenSSID = true;
//			config.wepKeys[0] = "\"" + Password + "\"";
//			config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
//			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
//			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
//			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
//			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
//			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
//			config.wepTxKeyIndex = 0;
//		}
//		if (type == SECURITY_PSK) { // WIFICIPHER_WPA
//			config.preSharedKey = "\"" + Password + "\"";
//			config.hiddenSSID = true;
//			config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
//			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
//			config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
//			config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
//			config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
//			config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
//			config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
//			config.status = WifiConfiguration.Status.ENABLED;
//		}
//		return config;
//	}
//}
